package com.bdmc.smokesensor.controller;

import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.Utils.http.HttpAPIService;
import com.bdmc.smokesensor.Utils.http.HttpResult;
import com.bdmc.smokesensor.entity.Smk_Alarm;
import com.bdmc.smokesensor.entity.Sys_Log;
import com.bdmc.smokesensor.services.Smk_AlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/alarm", method = RequestMethod.POST)
public class Smk_AlarmController extends BaseController {

    @Autowired
    private Smk_AlarmService _alarmSvs;

    @Autowired
    private HttpAPIService _apiSvs;

    @RequestMapping(value = "/list")
    public Msg list(@RequestBody Map<String, Object> reqMap) {
        int page = Integer.parseInt(reqMap.get("page").toString());
        int rows = Integer.parseInt(reqMap.get("rows").toString());
        int dep_id = Integer.parseInt(reqMap.get("dep_id").toString());
        int gc_id = Integer.parseInt(reqMap.get("gc_id").toString());
        int alarmType = Integer.parseInt(reqMap.get("alarmType").toString());
        int alarmStat = Integer.parseInt(reqMap.get("alarmStat").toString());
        String start = reqMap.get("start").toString();
        String end = reqMap.get("end").toString();

        if (dep_id == 0) {
            dep_id = Integer.valueOf(request.getAttribute("dep_id").toString());
        }
        Map<String, Object> result = _alarmSvs.findByPageable(dep_id, alarmType, alarmStat, start, end, gc_id, page,
                rows);
        return ResultUtil.success(result);
    }

    @RequestMapping(value = "/count")
    public Msg count() {
        int dep_id = Integer.valueOf(request.getAttribute("dep_id").toString());
        Map<String, Object> result = _alarmSvs.findCountBydepId(dep_id);
        return ResultUtil.success(result);
    }

    @RequestMapping(value = "/xy")
    public Msg xy(@RequestBody Map<String, Object> reqMap) {
        try {
            String deviceId = reqMap.get("deviceId").toString();
            HttpResult re = _apiSvs.doGetCommand(deviceId, "5859");
           //HttpResult re = _apiSvs.doGetDevDetail(deviceId);
           if(re.getCode()==200){
            return ResultUtil.success("消音成功。");
           }else{
            return ResultUtil.error(-1,"消音失败。");
           }
           
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(-1,e.getMessage());
		}
    }

    /**
     * 数据新增或更新，save方法可以执行添加也可以执行更新，如果需要执行持久化的实体存在主键值则更新数据，如果不存在则添加数据。
     */

    @RequestMapping(value = "/save")
    public Msg save(@RequestBody Smk_Alarm al) {
        Smk_Alarm g = _alarmSvs.save(al);
        if (g != null && g.getId() > 0) {
            Sys_Log log = new Sys_Log(getLoginName(), getLocalIp(), "告警监控", "处理报警：" + g.getAlarmDetail());
            _logSvs.save(log);
            return ResultUtil.success();
        } else {
            return ResultUtil.error(-1, "操作失败，请联系管理员。");
        }
    }

    /**
     * 删除设备信息，删除信息后返回剩余信息
     */
    @RequestMapping(value = "/delete")
    public Msg delete(@RequestBody Map<String, Integer> map) {
        int id = Integer.valueOf(map.get("id"));
        _alarmSvs.deleteById(id);
        return ResultUtil.success();
    }

}